package com.dc.common.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.dc.system.model.SysUser;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.dc.base.exception.BusinessException;
import com.dc.common.constants.ConstantsCommon;


/**
 * 日志记录切面
 *
 * @author Shen
 * @date 2018年4月10日 下午1:26:29
 */
@Component
@Aspect
public class LoginAspect {

    @Pointcut("@annotation(com.dc.common.aop.LoginAop)")
    public void loginAspect() {
    }

    @Before("loginAspect() && @annotation(data)")
    public void doBefore(JoinPoint point, LoginAop data) throws Throwable {
        // 获取request以及session
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        // 获得当前用户
        SysUser user = (SysUser) session.getAttribute(ConstantsCommon.LOGIN_INFO);
        if (user == null) {
            throw new BusinessException(9, "登陆超时，请重新登陆");
        }

    }


}
